package com.logic.saas.action;

import java.util.Arrays;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import com.logic.saas.enums.KindEnum;
import com.logic.saas.pojo.Kind;
import com.logic.saas.service.EnumsService;
import com.logic.saas.util.AddLog;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/enums")
public class EnumsAction extends BaseAction implements ModelDriven<Kind> {

	
	/**
	 * update all enums to database
	 */
	
	@Autowired
	private EnumsService enumService;
	
	@Override
	@SkipValidation

	@Action(value="enums",results={@Result(name="success", location = "/WEB-INF/jsp/common/enum.jsp")})
	public String execute() {
		//this.synchronizeAllEnums();
		return SUCCESS;
	}

	@Action(value="synchronizeAllEnums",results={@Result(name="success", location = "/WEB-INF/jsp/common/enum.jsp")})
	public String synchronizeAllEnums(){
		AddLog.info(EnumsAction.class, "synchronizeAllEnums action start");
		
		try {
		   boolean synSuccess = enumService.updateAllEnums();
		   if(!synSuccess){
			   this.addActionError("Update failed");
		   }
		} catch(DataAccessException de){
			 this.addActionError(de.getMessage());
			de.printStackTrace();
		} catch (Exception e) {
			 this.addActionError(e.getMessage());
			e.printStackTrace();
		}
		//context.removeAttribute(ConstantEnum.ENUMS.getValue());
		//super.prepareCache().resetEnum();
		return SUCCESS;
	}
	

	public String getAllEnumsKind(){
		List list = Arrays.asList(KindEnum.values());
		this.setResult(list);
		return null;
	}
	
	public String deletedEnums(){
		try {
			//enumService.deleteKind(ids);
		} catch (Exception e) {
			AddLog.error(this.getClass(), e.toString());
		}
		return null;
	}
	
	public String getKindName() {
		return kindName;
	}

	public void setKindName(String kindName) {
		this.kindName = kindName;
	}

	public String getIds() {
		return ids;
	}


	public void setIds(String ids) {
		this.ids = ids;
	}

	public Kind getModel() {
		return enums;
	}

	public String getIsDeleted() {
		return isDeleted;
	}


	public void setIsDeleted(String isDeleted) {
		this.isDeleted = isDeleted;
	}




	private static final long serialVersionUID = 327601602753927279L;
	private Kind enums = new Kind();
	private String kindName;
	private String ids;
	private String isDeleted;
}
